
/*
 **  ITSessionMgr.m
 **
 **  Copyright (c) 2002, 2003
 **
 **  Author: Fabian, Ujwal S. Sathyam
 **	     Initial code by Kiichi Kusama
 **
 **  Project: iTerm
 **
 **  Description: manages an array of sessions.
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation; either version 2 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program; if not, write to the Free Software
 **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#import "ITSessionMgr.h"

@implementation ITSessionMgr

+ (id)sharedInstance;
{
    static ITSessionMgr* shared = nil;
    
    if (shared == nil)
        shared = [[ITSessionMgr alloc] init];
    
    return shared;
}

- (id)init;
{
    self = [super init];
    
    _sessionList = [[NSMutableArray alloc] init];
    _threadLock = [[NSLock alloc] init];
    
    return self;
}

- (void)dealloc;
{
    [_threadLock lock];
    
    int i, cnt = [_sessionList count];
    for(i = 0; i < cnt; i++)
        [[_sessionList objectAtIndex: i] terminate];
    
    [_sessionList release];
    _sessionList = nil;
    
    [_threadLock unlock];
    [_threadLock release];
        
    [_currentSession release];
    
    [super dealloc];
}

// creates a PuesdoTerminal session and inserts it into a NSView

- (PTYSession *)createSession:(NSView*)view {
    ITSessionDesc *sessionDesc = [ITSessionDesc defaultDesc];
	return [self createSession:view withSession:sessionDesc];
}

- (PTYSession *)createSession:(NSView*)view withSession:(ITSessionDesc *) sessionDesc {
	// Initialize a new session
    PTYSession *aSession = [[[PTYSession alloc] init] autorelease];
	
	NSString *title = @"title";
	
	// Init the rest of the session
    //[aSession setParent: self];
    [aSession initScreen:[view bounds]];
	VT100Screen* screen = [aSession SCREEN];
    
    // set some default parameters
    [aSession setPreferencesFromSessionDesc:sessionDesc];
    
    // set the srolling
    [[aSession scrollView] setVerticalLineScroll: [screen characterSize].height];
    [[aSession scrollView] setVerticalPageScroll: [[aSession TEXTVIEW] frame].size.height];
    
    // Set the bell option
    [VT100Screen setPlayBellFlag: ![[ITPreferences sharedInstance] silenceBell]];
	
    // assign terminal and task objects
    [screen setTerminal:[aSession TERMINAL]];
    [screen setShellTask:[aSession shell]];
    [[aSession TEXTVIEW] setDataSource: screen];
    [screen setTextStorage:[[aSession TEXTVIEW] textStorage]];
    
    [screen setScrollback:[[ITPreferences sharedInstance] numScrollbackLines]];
    
    // initialize the screen
    [screen initScreen];
    
	[[ITPreferences sharedInstance] setBlinkCursor:YES];
	
    [aSession startSession:title blinkCursor:[[ITPreferences sharedInstance] blinkCursor]];
	
    [view addSubview:[aSession scrollView]]; 
	
	[[aSession scrollView] setVerticalPageScroll: 0.0];
	
    [view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
    [view setAutoresizesSubviews:YES];	
	
    [[aSession scrollView] setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
    [[aSession scrollView] setAutoresizesSubviews:YES];	
	_currentSession = aSession;
	
	[self insertSession:aSession atIndex:[_sessionList count]];
	
	return aSession;
}


- (int)currentSessionIndex;
{
    return _currentSessionIndex;
}

- (PTYSession *)currentSession;
{
    return _currentSession;
}

- (void)setCurrentSession:(PTYSession *)session;
{
    _currentSessionIndex = [_sessionList indexOfObject:session];
    
    if (_currentSessionIndex != NSNotFound)
    {
        [_currentSession autorelease];
        _currentSession = [session retain];    
    }
}

- (void)setCurrentSessionIndex:(int)index;
{
    if (index >= 0 && index < [_sessionList count])
        [self setCurrentSession:[self sessionAtIndex:index]];
}

- (NSArray*)sessionList;
{
    return _sessionList;
}

- (void)removeSession:(PTYSession*)session;
{
    int removeIndex = [_sessionList indexOfObject:session];
    if (removeIndex != NSNotFound)
    {
        [_threadLock lock];
        [_sessionList removeObjectAtIndex:removeIndex];
        [_threadLock unlock];

        if (removeIndex >= [_sessionList count])
            removeIndex = [_sessionList count] - 1;
        
        [self setCurrentSessionIndex:removeIndex];
    }
}

- (void)insertSession:(PTYSession*)session atIndex:(int)index
{
    if (index > [_sessionList count])
        index = [_sessionList count];
    
    [_threadLock lock];
    [_sessionList insertObject:session atIndex:index];
    [_threadLock unlock];

    if (_currentSessionIndex >= index)
        [self setCurrentSessionIndex:_currentSessionIndex+1];
}

- (unsigned)numberOfSessions;
{
    return [_sessionList count];
}

- (void)replaceSessionAtIndex:(int)index withSession:(PTYSession*)session;
{
    [_threadLock lock];

    [_sessionList replaceObjectAtIndex:index withObject:session];
    
    [_threadLock unlock];
}

- (PTYSession*)sessionAtIndex:(unsigned)index;
{
    if (index >= 0 && index < [_sessionList count])
        return [_sessionList objectAtIndex:index];
    
    return nil;
}

- (BOOL)containsSession:(PTYSession *)session;
{
    return [_sessionList containsObject:session];
}

+ (void)breakDown:(NSString *)cmdl cmdPath:(NSString **)cmd cmdArgs:(NSArray **)path;
{
    int i,j,k,qf;
    char tmp[100];
    const char *s;
    NSMutableArray *p;
    
    p = [[NSMutableArray alloc] init];
    
    s = [cmdl UTF8String];
    
    i= j = qf= 0;
    k = -1;
    
    while (i <= strlen(s)) 
    {
        if (qf) 
        {
            if (s[i] == '\"')
                qf = 0;
            else
                tmp[j++] = s[i];
        }
        else 
        {
            if (s[i] == '\"')
                qf = 1;
            else if (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == 0)
            {
                tmp[j] = 0;
                if (k == -1)
                    *cmd = [NSString stringWithUTF8String:tmp];
                else
                    [p addObject:[NSString stringWithUTF8String:tmp]];
                
                j=0;
                k++;
                
                while (s[i+1] == ' ' || s[i+1] == '\t' || s[i+1] == '\n' || s[i+1]==0)
                    i++;
            }
            else
                tmp[j++] = s[i];
        }
        
        i++;
    }
    
    *path = [NSArray arrayWithArray:p];
    [p release];
}


@end
